package fun.lifepoem.blog.controller;

import fun.lifepoem.blog.common.GitEnum;
import fun.lifepoem.blog.domain.dto.gitee.GiteeFallbackBean;
import fun.lifepoem.blog.service.GiteeFallbackService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @author Yiwyn
 * @create 2022/9/18 14:49
 */
@Slf4j
@RestController
@RequestMapping("/gitee")
public class GiteeFallbackController {


    @Autowired
    private GiteeFallbackService giteeFallbackService;

    //进行更新的分支
    private static final List<String> ignoreBanches
            = Arrays.asList(GitEnum.BRANCHE_MAIN.getTypeName(), GitEnum.BRANCHE_MASTER.getTypeName());

    @PostMapping("/pushFallback")
    public void giteePushFallback(HttpServletRequest request, @RequestBody GiteeFallbackBean payload) throws IOException {
        log.info("回调信息request:{}, requestBody:{}", request, payload);
        String ref = payload.getRef();
        log.info("push回调,当前提交分支:{}", ref);
        String[] refs = payload.getRef().split("/");
        if (refs.length != 0) {
            String branche = refs[refs.length - 1];
            if (ignoreBanches.contains(branche)) {
                giteeFallbackService.giteePushFallback(payload);
            }
        }
    }

    @GetMapping("/test")
    public String test() {
        return "Hello Gitee";
    }

}
